<template>
  <div>
    <el-table :data="ArticleData" border style="width: 100%">
      <el-table-column prop="id" label="ID" width="80" align="center"></el-table-column>
      <el-table-column prop="title" label="文章标题" align="center" width="150"></el-table-column>
      <el-table-column prop="brief" label="文章摘要" :show-overflow-tooltip="true" header-align="center"width="100" show-overflow-tooltip></el-table-column>
      <el-table-column prop="content" label="文章内容" :show-overflow-tooltip="true" align="center" ></el-table-column>
      <el-table-column prop="reference" label="参考资料" :show-overflow-tooltip="true" align="center" width="100"></el-table-column>
      <el-table-column prop="viewCount" label="查看次数" align="center" width="100"></el-table-column>
      <el-table-column label="操作" align="center" width="120">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" size="mini" circle
                     @click="openEditDialog(scope.row)"></el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini" circle
                     @click="openDeleteConfirm(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>

<!--    &lt;!&ndash; 分页控件 &ndash;&gt;
    <div style="text-align: right; margin: 10px 0 0 0; height: 30px;"
  <el-button size="mini" style="float: left;"
                 v-if="history.length > 0"
                 @click="goBack">返回
      </el-button>
      <el-pagination
          @current-change="changePage"
          hide-on-single-page
          :current-page="currentPage"
          :page-size="pageSize"
          layout="total, prev, pager, next"
          :total="total">
      </el-pagination>-->
  </div>
</template>

<script>
import BaseUrl from "@/http/BaseUrl";
import TokenAxios from "@/http/TokenAxios";
import axios from "axios";

export default {
  data() {
    return {
      labelPosition: 'right',
      ArticleData: [],
      type:'1'
    };
  },
  methods: {
    loadCurrentLoginInfo() {
      let loginInfoString=localStorage.getItem('loginInfo')

      this.loginInfo = JSON.parse(loginInfoString);
      if (!this.loginInfo) {
        this.$router.push('/login/user');
        return;
      }else if (this.loginInfo.role!==3&&this.loginInfo.role!==4){
        this.$message.error('无权访问');
        this.$router.push('/home');
        return;
      }
    },
    // 加载文章列表
    loadArticleList() {
      let articleUrl = BaseUrl.POST_DOCTOR + '/article/getArticleList' ;
      console.log('articleUrl = ' + articleUrl);
      TokenAxios.get(articleUrl).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state === 20000) {
          // JSON.parse(jsonResult);
          this.ArticleData = jsonResult.data;

          console.log('ArticleData = ' + this.ArticleData)
        } else {
          let title = '操作失败';
          this.$alert(jsonResult.message, title, {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      }).catch(error => {
        let title = '错误';
        let message = '程序执行过程中出现错误，请打开浏览器的控制台查看详细错误信息！';
        this.$alert(message, title, {
          confirmButtonText: '确定',
          callback: action => {
          }
        });
        console.log(error);
      });
    },
    // 打开删除确认框
    openDeleteConfirm(ArticleData) {
      let message = '将永久删除文章名为'+ArticleData.title+'的文章，是否继续？';
      this.$confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(ArticleData);
      }).catch(() => {
      });
    },
    // 执行删除
    handleDelete(ArticleData) {
      let url = BaseUrl.POST_DOCTOR + '/article/' + ArticleData.id + '/delete';
      console.log('url = ' + url);
      TokenAxios.post(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state === 20000) {
          this.$message({
            message: '删除成功！',
            type: 'success'
          });
          this.loadArticleList();
        } else if (jsonResult.state === 40400) {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadArticleList();
            }
          });
        } else {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });
    },
  },
  mounted() {
    this.loadCurrentLoginInfo();
    this.loadArticleList();
  }
}
</script>
